<?php
namespace app\app\controller;

use app\common\controller\App;
use think\Db;
use think\Exception;


class Change extends App
{
    protected $middleware = ['AppAuth'];

    //我的换宿申请
    public function getList()
    {

        $uid=$this->getLoginUid();
        $list=Db::name("ChangeRoom")
            ->where(['uid'=>$uid])
            ->order("id desc")
            ->select();
        if (!empty($list)){
            foreach ($list as $key=>$value){
            }
        }

        return $this->ok('请求成功',$list);

    }

    //我的换宿详情
    public function detail()
    {
        $id=request()->param("id",'0','intval');
        $uid=$this->getLoginUid();
        $detail=Db::name("ChangeRoom")->where(['uid'=>$uid,'id'=>$id])->find();
        if (empty($detail)){
            return $this->fail('申请不存在');
        }else{
            $detail['create_time']=date("Y年m月d日",$detail['create_time']);
            $detail['house']=Db::name("House")->where(['id'=>$detail['house_id']])->value('title');
            $detail['room']=Db::name("Room")->where(['id'=>$detail['room_id']])->value('title');

            $status=model("ChangeRoom")::STATUS;
            $detail['status_title']=isset($status[$detail['status']])?$status[$detail['status']]:'';

            $step=[
                ['name'=>'等待老师处理'],
                ['name'=>'等待院长处理'],
                ['name'=>'等待宿管安排'],
                ['name'=>'换宿成功'],

            ];
//            0待处理, 1老师同意，2老师拒绝，3院长同意，4院长拒绝
           $num=0;
            switch ($detail['status']){
                case 0: $num=0;
                    break;
                case 1: $num=1;
                    break;
                case 2: $num=0;
                    break;
                case 3: $num=2;
                    break;
                case 4: $num=1;
                    break;
                case 6:$num=3;
                    break;
                default:$num=0;
                    break;

            }
            $data=[
                'detail'=>$detail,
                'step'=>$step,
                'num'=>$num
            ];

            return $this->ok('请求成功',$data);
        }
    }

    //删除
    public function del()
    {
        $id=request()->param("id",'0','intval');
        $uid=$this->getLoginUid();
        $res=model("Jianyi")->where(['uid'=>$uid,'id'=>$id])->delete();
        if ($res){
            return $this->ok('删除成功');
        }else{
            return $this->fail('删除失败');
        }
    }


    public function get_house()
    {
        $house=Db::name("House")->order("id desc")->select();
        return $this->ok('请求成功',$house);

    }

    public function get_room()
    {
        $id=request()->param("house_id",'0','intval');//宿舍楼id
        $room_list=Db::name('Room')->where(['house_id'=>$id])->order("id desc")->select();
        if (!empty($room_list)){
            foreach ($room_list as $key=>$val){
                $res_bed=$val['count']-$val['has_people'];
                $room_list[$key]['res_bed']=$res_bed;
                $room_list[$key]['title']=$val['title']."(剩余床位".$res_bed.")张";
            }
        }

        return $this->ok('请求成功',$room_list);
    }


    //提交换宿申请
    public function add()
    {
        $user_info=$this->getAppLoginInfo();
        $data['house_id']=request()->param("house_id",'0','intval');
        $data['room_id']=request()->param("room_id",'0','intval');
        $data['uid']=$user_info['uid'];
        $data['create_time']=time();
        $data['reason']=request()->param("reason");
        if (empty($data['house_id'])){
            return $this->fail('请选择要换的宿舍楼');
        }
        if (empty($data['room_id'])){
            return $this->fail('请选择要更换的宿舍');
        }
        if (empty($data['reason'])){
            return $this->fail('请填写更换理由');
        }


        if ($data['house_id']==$user_info['house_id'] && $data['room_id']==$user_info['sushe_id'])
        {
            return $this->fail('不能调换自己宿舍');
        }

        $change_room=Db::name("ChangeRoom")->where(['res'=>0,'uid'=>$user_info['uid']])->count();
        if ($change_room==1){
            return $this->fail('您已提交过换宿申请请勿多次提交');
        }

        $room=Db::name('Room')->where(['id'=>$data['room_id']])->find();
        $res_bed=$room['count']-$room['has_people'];
        if ($res_bed<1){
            return $this->fail('该宿舍已经没有床位了');
        }

        $house=Db::name("House")->where(['id'=>$data['house_id']])->value('title');
        $title="申请换宿".$house.$room['title'];
        $data['title']=$title;
        $res=Db::name("ChangeRoom")->insert($data);
        if ($res){
            $message="您".$title.'已提交成功,等待学院处理';
            model("Message")->add_message($message,$user_info['uid']);

        }else{
            return $this->fail('申请失败');
        }

    }


}
